package com.bocheng.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bocheng.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service(value = "userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //判断非空
        if(StrUtil.isEmpty(username)){
            throw new RuntimeException("用户名不能为空");
        }
        //如果不为空,则从数据库中通过用户名获取对应的用户信息
        QueryWrapper<com.bocheng.entity.User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        com.bocheng.entity.User sysUser = userMapper.selectOne(wrapper);
        if(sysUser==null){
            throw new UsernameNotFoundException(String.format("%s这个用户不存在",username));
        }
        //如果用户存在则要查询用户的角色集合
        List<GrantedAuthority> authorities = new ArrayList<>();
        String codeList= sysUser.getRole();
            //如果数据库中没有添加ROLE_则这里需要添加一个
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_"+codeList);
            authorities.add(authority);
        return new User(sysUser.getUsername(),sysUser.getPassword(),authorities);
    }


}